package net.yunzhanyi.client.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.yunzhanyi.client.domain.dto.ClientLoginUser;
import net.yunzhanyi.client.domain.dto.PoetryDto;
import net.yunzhanyi.client.domain.vo.PageVo;
import net.yunzhanyi.client.domain.vo.PoetryVo;
import net.yunzhanyi.client.service.CollectionService;
import net.yunzhanyi.client.service.PoetryService;
import net.yunzhanyi.client.utils.AuthUtils;
import net.yunzhanyi.common.core.utils.ObjectUtils;
import net.yunzhanyi.common.web.util.StringUtils;
import net.yunzhanyi.generic.domain.model.*;
import net.yunzhanyi.generic.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author bestct
 * @date 2022/9/28
 * @type 类
 */
@Service
public class PoetryServiceImpl implements PoetryService {
    @Autowired
    private PoetryMapper poetryMapper;

    @Autowired
    private AuthorMapper authorMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private DynastyMapper dynastyMapper;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private PartMapper partMapper;

    @Override
    public PageInfo<Poetry> searchPoetry(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Poetry> poetries = poetryMapper.selectByKeyword(keyword);
        return new PageInfo<>(poetries);
    }

    @Override
    public List<String> searchAuthorTip(String keyword) {
        return null;
    }

    @Override
    public List<String> searchPoetryTip(String keyword) {
        return null;
    }

    @Override
    public PageInfo<Author> searchAuthor(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Author> authors = authorMapper.selectByKeyword(keyword);
        return new PageInfo<>(authors);
    }

    @Override
    public Author searchAuthorById(Integer authorId) {
        return null;
    }

    @Override
    public List<Poetry> searchAuthorPoetry(Integer authorId, Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public List<Part> searchPartRandom(int randomSize) {

        long[] ints = new long[randomSize];
        Random rand = new Random();
        int r;
        for (int i = 0; i < randomSize; i++) {
            r = rand.nextInt(6734) + 1;
            ints[i] = r;
        }
        return partMapper.selectByPrimaryKeys(ints);
    }

    @Override
    public List<Author> searchAuthorRandom(int pageSize) {
        Integer count = authorMapper.selectCount();
        Random rand = new Random();
        int pageNum = rand.nextInt(count / pageSize) + 1;
        int offset = (pageNum - 1) * pageSize;
        List<Author> authors = authorMapper.selectAuthorSimple(offset, pageSize);
        return authors;
    }

    @Override
    public PoetryVo searchWebPoetryById(Long poetryId) {
        Poetry poetry = poetryMapper.selectWithDetailsByPrimaryKey(poetryId);
        if (poetry == null) {
            return null;
        }
        PoetryVo poetryVo = new PoetryVo();
        BeanUtils.copyProperties(poetry, poetryVo);
        String tags = poetry.getTags();
        if (StringUtils.isNotEmpty(tags)) {
            String[] split = tags.split(";");
            List<Tag> tagList = new ArrayList<>();
            for (String name : split) {
                Tag tag = tagMapper.selectByName(name);
                tagList.add(tag);
            }
            poetryVo.setTagList(tagList);
        }
        ClientLoginUser loginUser = AuthUtils.getLoginUser();
        long uid = -1;

        if (ObjectUtils.nonNull(loginUser)) {
            uid = loginUser.getUid();
        }
        poetryVo.setCollection(collectionService.isCollected(uid, poetryId, 1));
        Author author = authorMapper.selectByPrimaryKey(poetry.getAuthorId());
        poetryVo.setAuthor(author);
        return poetryVo;
    }

    @Override
    public PageInfo<Poetry> indexPoetry(Integer dynasty, Integer tagId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Poetry> poetries = poetryMapper.selectAllPoetry(dynasty, tagId);
        return new PageInfo<>(poetries);
    }

    @Override
    public List<Tag> initTag() {
        List<Tag> tags = tagMapper.selectInit();
        return tags;
    }

    @Override
    public List<Dynasty> initDynasty() {
        List<Dynasty> dynasties = dynastyMapper.selectInit();
        return dynasties;
    }

    @Override
    public PageVo<PoetryDto> copyPageInfo(PageInfo<Poetry> poetryInfo) {
        List<Poetry> poetries = poetryInfo.getList();
        List<PoetryDto> poetryDtos = new ArrayList<>();
        if (poetries != null) {
            for (Poetry poetry : poetries) {
                PoetryDto poetryDto = new PoetryDto();
                BeanUtils.copyProperties(poetry, poetryDto);
                String tags = poetry.getTags();
                if (StringUtils.isNotEmpty(tags)) {
                    String[] split = tags.split(";");
                    List<Tag> tagList = new ArrayList<>();
                    for (String name : split) {
                        Tag tag = tagMapper.selectByName(name);
                        tagList.add(tag);
                    }
                    poetryDto.setTagList(tagList);
                }
                poetryDtos.add(poetryDto);
            }
        }
        PageVo<PoetryDto> poetryPageVo = new PageVo<>(poetryDtos);
        poetryPageVo.setPageNum(poetryInfo.getPageNum());
        poetryPageVo.setPages(poetryInfo.getPages());
        poetryPageVo.setNavigatepageNums(poetryInfo.getNavigatepageNums());
        poetryPageVo.setPageSize(poetryInfo.getPageSize());
        return poetryPageVo;
    }

    @Override
    public List<Tag> checkTagId(Integer tagId, List<Tag> tagVos) {
        for (Tag tag : tagVos) {
            if (tagId.equals(tag.getTagId())) {
                return tagVos;
            }
        }
        Tag tag = tagMapper.selectByPrimaryKey(tagId);
        tagVos.add(0, tag);
        return tagVos;
    }

    @Override
    public PageInfo<Author> indexAuthor(Integer dynasty, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Author> authors = authorMapper.selectAllAuthor(dynasty);
        return new PageInfo<>(authors);
    }

    @Override
    public PageInfo<Poetry> getPoetryByAuthorId(Long authorId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Poetry> poetries = poetryMapper.selectByAuthorId(authorId);
        return new PageInfo<>(poetries);
    }

    @Override
    public List<Poetry> searchPoetryRandom(int size) {
        Integer count = poetryMapper.selectCount();
        Random rand = new Random();
        int pageNum = rand.nextInt(count / size) + 1;

        int offset = (pageNum - 1) * size;
        System.out.println("offset = " + offset);
        List<Poetry> poetries = poetryMapper.selectPoetrySimple(offset, size);
        System.out.println(poetries);
        return poetries;
    }

    @Override
    public Poetry getPoetryByRandom() {
        Integer count = poetryMapper.selectCount();
        Random rand = new Random();
        int offset = rand.nextInt(count ) + 1;
        return poetryMapper.selectRandomPoetry(offset);
    }

}
